let express = require("express");
let path = require("path");
let fs = require("fs");
let OSS = require("ali-oss");
const router = express.Router();
const log4js  = require("../config/log.js");
let logger=log4js.getLogger("info")
//获取客户端ip地址
var ip=require("../public/getIp.js");
//公告
//获取oss中str1
router.post("/getOssStr",(req,res)=>{
	if (req.body.server === "安卓测试服") {
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-android',
		});
		async function getBuffer(){
			try {
				let result = await client.get('Debug/notice.json');
				//字符串转换成对象
				// let obj = eval('('+result.content.toString()+')');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeAcs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let noticeObj = JSON.parse(data);
							res.send({
								code: 200,
								msg: "success",
								result: noticeObj
							})
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	}
	if (req.body.server === "安卓正试服") {
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-android',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Release/notice.json');
				//字符串转换成对象
				// let obj = eval('('+result.content.toString()+')');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeAzs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let noticeObj = JSON.parse(data);
							res.send({
								code: 200,
								msg: "success",
								result: noticeObj
							})
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	} 
	if (req.body.server === "ios测试服") {
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-ios',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Debug/notice.json');
				//字符串转换成对象
				// let obj = eval('('+result.content.toString()+')');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeIcs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let noticeObj = JSON.parse(data);
							res.send({
								code: 200,
								msg: "success",
								result: noticeObj
							})
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	}
	if (req.body.server === "ios正试服") {
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-ios',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Release/notice.json');
				//字符串转换成对象
				// let obj = eval('('+result.content.toString()+')');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeIzs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let noticeObj = JSON.parse(data);
							res.send({
								code: 200,
								msg: "success",
								result: noticeObj
							})
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	}
})
//获取oss服务内容
router.post("/getOss", (req, res) => {
	const logObj={
		date:req.body.date,
		user_actions:req.body.user_actions,
		ip4:ip.getClientIp(req)
	}
	logger.info(logObj);
	if (req.body.server === "安卓测试服") {
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-android',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Debug/notice.json');
				//字符串转换成对象
				// let obj = eval('('+result.content.toString()+')');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeAcs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let obj={}
							let noticeObj = JSON.parse(data);
							for(let i in noticeObj){
								if(i==req.body.tabs){
									if(JSON.stringify(noticeObj[i])==="{}"){
										res.send({
											code:400,
											msg:"没有对应的Tab数据"
										})
									}else{
										obj={
											cnName:noticeObj[i].Name.CHmessage,
											cnContent:noticeObj[i].Content.CHmessage,
											enName:noticeObj[i].Name.ENmessage,
											enContent:noticeObj[i].Content.ENmessage,
											frenchName:noticeObj[i].Name.FRmessage,
											frenchContent:noticeObj[i].Content.FRmessage,
											germanName:noticeObj[i].Name.DEmessage,
											germanContent:noticeObj[i].Content.DEmessage,
											russianName:noticeObj[i].Name.RUmessage,
											russianContent:noticeObj[i].Content.RUmessage
										}
										res.send({
											code: 200,
											msg: "success",
											result: obj
										})
									}
								}
							}
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	} 
	if (req.body.server === "安卓正试服") {
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-android',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Release/notice.json');
				//字符串转换成对象
				// let obj = eval('('+result.content.toString()+')');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeAzs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let obj={}
							let noticeObj = JSON.parse(data);
							for(let i in noticeObj){
								if(i==req.body.tabs){
									if(JSON.stringify(noticeObj[i])==="{}"){
										res.send({
											code:400,
											msg:"没有对应的Tab数据"
										})
									}else{
										obj={
											cnName:noticeObj[i].Name.CHmessage,
											cnContent:noticeObj[i].Content.CHmessage,
											enName:noticeObj[i].Name.ENmessage,
											enContent:noticeObj[i].Content.ENmessage,
											frenchName:noticeObj[i].Name.FRmessage,
											frenchContent:noticeObj[i].Content.FRmessage,
											germanName:noticeObj[i].Name.DEmessage,
											germanContent:noticeObj[i].Content.DEmessage,
											russianName:noticeObj[i].Name.RUmessage,
											russianContent:noticeObj[i].Content.RUmessage
										}
										res.send({
											code: 200,
											msg: "success",
											result: obj
										})
									}
								}
							}
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	} 
	if (req.body.server === "ios测试服") {
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-ios',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Debug/notice.json');
				//字符串转换成对象
				// let obj = eval('('+result.content.toString()+')');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeIcs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let obj={}
							let noticeObj = JSON.parse(data);
							for(let i in noticeObj){
								if(i==req.body.tabs){
									if(JSON.stringify(noticeObj[i])==="{}"){
										res.send({
											code: 400,
											msg: "没有对应tab数据"
										})
									}else{
										obj={
											cnName:noticeObj[i].Name.CHmessage,
											cnContent:noticeObj[i].Content.CHmessage,
											enName:noticeObj[i].Name.ENmessage,
											enContent:noticeObj[i].Content.ENmessage,
											frenchName:noticeObj[i].Name.FRmessage,
											frenchContent:noticeObj[i].Content.FRmessage,
											germanName:noticeObj[i].Name.DEmessage,
											germanContent:noticeObj[i].Content.DEmessage,
											russianName:noticeObj[i].Name.RUmessage,
											russianContent:noticeObj[i].Content.RUmessage
										}
										res.send({
											code: 200,
											msg: "success",
											result: obj
										})
									}
								}
							}
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	}
	if (req.body.server === "ios正试服") {
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-ios',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Release/notice.json');
				//字符串转换成对象
				// let obj = eval('('+result.content.toString()+')');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeIzs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let obj={}
							let noticeObj = JSON.parse(data);
							for(let i in noticeObj){
								if(i==req.body.tabs){
									if(JSON.stringify(noticeObj[i])==="{}"){
										res.send({
											code:400,
											msg:"没有对应的Tab数据"
										})
									}else{
										obj={
											cnName:noticeObj[i].Name.CHmessage,
											cnContent:noticeObj[i].Content.CHmessage,
											enName:noticeObj[i].Name.ENmessage,
											enContent:noticeObj[i].Content.ENmessage,
											frenchName:noticeObj[i].Name.FRmessage,
											frenchContent:noticeObj[i].Content.FRmessage,
											germanName:noticeObj[i].Name.DEmessage,
											germanContent:noticeObj[i].Content.DEmessage,
											russianName:noticeObj[i].Name.RUmessage,
											russianContent:noticeObj[i].Content.RUmessage
										}
										res.send({
											code: 200,
											msg: "success",
											result: obj
										})
									}
								}
							}
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	}
});
//保存公告
router.post("/saveOss", (req, res) => {
	const logObj={
		date:req.body.date,
		user_actions:req.body.user_actions,
		ip4:ip.getClientIp(req)
	}
	logger.info(logObj);
	if (req.body.server === "安卓测试服") {
		let obj = req.body.obj;
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-android',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Debug/notice.json');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeAcs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let noticejson = JSON.parse(data);
							for (let i in noticejson) {
								if (i === "Tab8") {
									res.send({
										code: 400,
										msg: "新增的公告大于了7个"
									});
									return;
								} else {
									if (i === "str1") {
										noticejson[i] = req.body.str1;
									}
									if (i === req.body.tabs) {
										noticejson[req.body.tabs] = obj;
										let noticeaz = Buffer.from(JSON.stringify(noticejson));
										fs.writeFile(file, noticeaz, function(err) {
											if (err) {
												logger.info(err)
											} else {
												async function append() {
													try {
														await client.put('Debug/notice.json', 'map/noticeAcs.json');
													} catch (e) {
														logger.info(e);
													}
												}
												append();
												res.send({
													code: 200,
													msg: "公告保存成功"
												})
											}
										})
									}
								}
							}
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	}
	if (req.body.server === "安卓正试服") {
		let obj = req.body.obj;
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-android',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Release/notice.json');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeAzs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let noticejson = JSON.parse(data);
							for (let i in noticejson) {
								if (i === "Tab8") {
									res.send({
										code: 400,
										msg: "新增的公告大于了7个"
									});
									return;
								} else {
									if (i === "str1") {
										noticejson[i] = req.body.str1;
									}
									if (i === req.body.tabs) {
										noticejson[req.body.tabs] = obj;
										let noticeaz = Buffer.from(JSON.stringify(noticejson));
										fs.writeFile(file, noticeaz, function(err) {
											if (err) {
												logger.info(err)
											} else {
												async function append() {
													try {
														await client.put('Release/notice.json', 'map/noticeAzs.json');
													} catch (e) {
														logger.info(e);
													}
												}
												append();
												res.send({
													code: 200,
													msg: "公告保存成功"
												})
											}
										})
									}
								}
							}
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	}
	if (req.body.server === "ios测试服") {
		let obj = req.body.obj;
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-ios',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Debug/notice.json');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeIcs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let noticejson = JSON.parse(data);
							for (let i in noticejson) {
								if (i === "Tab8") {
									res.send({
										code: 400,
										msg: "新增的公告大于了7个"
									});
									return;
								} else {
									if (i === "str1") {
										noticejson[i] = req.body.str1;
									}
									if (i === req.body.tabs) {
										noticejson[req.body.tabs] = obj;
										let noticeaz = Buffer.from(JSON.stringify(noticejson));
										fs.writeFile(file, noticeaz, function(err) {
											if (err) {
												logger.info(err)
											} else {
												async function append() {
													try {
														await client.put('Debug/notice.json', 'map/noticeIcs.json');
													} catch (e) {
														logger.info(e);
													}
												}
												append();
												res.send({
													code: 200,
													msg: "公告保存成功"
												})
											}
										})
									}
								}
							}
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	}
	if (req.body.server === "ios正试服") {
		let obj = req.body.obj;
		const client = new OSS({
			region: 'oss-cn-hongkong',
			accessKeyId: 'LTAIs2pEj6nIwLAo',
			accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
			bucket: 'sd-ios',
		});
		async function getBuffer() {
			try {
				let result = await client.get('Release/notice.json');
				let notice = result.content.toString();
				let file = path.join(__dirname, '../map/noticeIzs.json');
				fs.writeFile(file, notice, function(err) {
					if (err) {
						console.error(err);
					} else {
						fs.readFile(file, 'utf8', function(err, data) {
							let noticejson = JSON.parse(data);
							for (let i in noticejson) {
								if (i === "Tab8") {
									res.send({
										code: 400,
										msg: "新增的公告大于了7个"
									});
									return;
								} else {
									if (i === "str1") {
										noticejson[i] = req.body.str1;
									}
									if (i === req.body.tabs) {
										noticejson[req.body.tabs] = obj;
										let noticeaz = Buffer.from(JSON.stringify(noticejson));
										fs.writeFile(file, noticeaz, function(err) {
											if (err) {
												logger.info(err)
											} else {
												async function append() {
													try {
														await client.put('Release/notice.json', 'map/noticeIzs.json');
													} catch (e) {
														logger.info(e);
													}
												}
												append();
												res.send({
													code: 200,
													msg: "公告保存成功",
												})
											}
										})
									}
								}
							}
						});
					}
				})
			} catch (e) {
				logger.info(e);
			}
		}
		getBuffer();
	}
});
//删除公告
router.post("/delOss", (req, res) => {
	const logObj={
		date:req.body.date,
		user_actions:req.body.user_actions,
		ip4:ip.getClientIp(req)
	}
	logger.info(logObj);
	if (req.body.server === "安卓测试服") {
		let file = path.join(__dirname, '../map/noticeAcs.json');
		fs.readFile(file, 'utf8', function(err, data) {
			if (err) {
				console.error(err);
			}
			let notice = JSON.parse(data);
			//把数据读出来,然后进行修改
			for (let i in notice) {
				if (i === req.body.Tab) {
					for (let key in req.body.tab) {
						if (notice[i][key]) {
							for (let item in req.body.tab[key]) {
								if (req.body.tab[key][item] === "") {
									notice[i] = {};
								} else {
									notice[i][key] = req.body.tab[key];
								}
							}
						}
					}
				}
			}
			let str = JSON.stringify(notice);
			let noticeaz = Buffer.from(str);
			fs.writeFile(file, noticeaz, function(err) {
				if (err) {
					console.error(err);
				} else {
					let client = new OSS({
						region: 'oss-cn-hongkong',
						accessKeyId: 'LTAIs2pEj6nIwLAo',
						accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
						bucket: 'sd-android',
					});
					async function append() {
						try {
							await client.put('Debug/notice.json', 'map/noticeAcs.json');
						} catch (e) {
							logger.info(e);
						}
					}
					append();
					res.send({
						code: 200,
						msg: "公告删除成功"
					})
				}
			})
		})
	} else if (req.body.server === "安卓正试服") {
		let file = path.join(__dirname, '../map/noticeAzs.json');
		fs.readFile(file, 'utf8', function(err, data) {
			if (err) {
				console.error(err);
			}
			let notice = JSON.parse(data);
			//把数据读出来,然后进行修改
			for (let i in notice) {
				if (i === req.body.Tab) {
					for (let key in req.body.tab) {
						if (notice[i][key]) {
							for (let item in req.body.tab[key]) {
								if (req.body.tab[key][item] === "") {
									notice[i] = {};
								} else {
									notice[i][key] = req.body.tab[key];
								}
							}
						}
					}
				}
			}
			let str = JSON.stringify(notice);
			let noticeaz = Buffer.from(str);
			fs.writeFile(file, noticeaz, function(err) {
				if (err) {
					console.error(err);
				} else {
					let client = new OSS({
						region: 'oss-cn-hongkong',
						accessKeyId: 'LTAIs2pEj6nIwLAo',
						accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
						bucket: 'sd-android',
					});
					async function append() {
						try {
							await client.put('Release/notice.json', 'map/noticeAzs.json');
						} catch (e) {
							logger.info(e);
						}
					}
					append();
					res.send({
						code: 200,
						msg: "公告删除成功"
					})
				}
			})
		})
	} else if (req.body.server === "ios测试服") {
		let file = path.join(__dirname, '../map/noticeIcs.json');
		fs.readFile(file, 'utf8', function(err, data) {
			if (err) {
				console.error(err);
			}
			let notice = JSON.parse(data);
			//把数据读出来,然后进行修改
			for (let i in notice) {
				if (i === req.body.Tab) {
					for (let key in req.body.tab) {
						if (notice[i][key]) {
							for (let item in req.body.tab[key]) {
								if (req.body.tab[key][item] === "") {
									notice[i] = {};
								} else {
									notice[i][key] = req.body.tab[key];
								}
							}
						}
					}
				}
			}
			let str = JSON.stringify(notice);
			let noticeaz = Buffer.from(str);
			fs.writeFile(file, noticeaz, function(err) {
				if (err) {
					console.error(err);
				} else {
					let client = new OSS({
						region: 'oss-cn-hongkong',
						accessKeyId: 'LTAIs2pEj6nIwLAo',
						accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
						bucket: 'sd-ios',
					});
					async function append() {
						try {
							await client.put('Debug/notice.json', 'map/noticeIcs.json');
						} catch (e) {
							logger.info(e);
						}
					}
					append();
					res.send({
						code: 200,
						msg: "公告删除成功"
					})
				}
			})
		})
	} else if (req.body.server === "ios正试服") {
		let file = path.join(__dirname, '../map/noticeIzs.json');
		fs.readFile(file, 'utf8', function(err, data) {
			if (err) {
				logger.info(err);
			}
			let notice = JSON.parse(data);
			//把数据读出来,然后进行修改
			for (let i in notice) {
				if (i === req.body.Tab) {
					for (let key in req.body.tab) {
						if (notice[i][key]) {
							for (let item in req.body.tab[key]) {
								if (req.body.tab[key][item] === "") {
									notice[i] = {};
								} else {
									notice[i][key] = req.body.tab[key];
								}
							}
						}
					}
				}
			}
			let str = JSON.stringify(notice);
			let noticeaz = Buffer.from(str);
			fs.writeFile(file, noticeaz, function(err) {
				if (err) {
					console.error(err);
				} else {
					let client = new OSS({
						region: 'oss-cn-hongkong',
						accessKeyId: 'LTAIs2pEj6nIwLAo',
						accessKeySecret: 'veSWlOOhMYLhP95j0VmAXK1ZVX06ae',
						bucket: 'sd-ios',
					});
					async function append() {
						try {
							await client.put('Release/notice.json', 'map/noticeIzs.json');
						} catch (e) {
							logger.info(e);
						}
					}
					append();
					res.send({
						code: 200,
						msg: "公告删除成功"
					})
				}
			})
		})
	}
});

module.exports = router;